package com.ctosb.classviewer.clazz;

import com.ctosb.classviewer.ClassReader;
import com.ctosb.classviewer.clazz.attr.Attribute_info;
import com.ctosb.classviewer.clazz.cp.CONSTANT_Utf8_info;
import com.ctosb.classviewer.clazz.util.Attribute_infoParse;
import lombok.Data;
import lombok.experimental.Accessors;

@Data
@Accessors(chain = true)
public class Field_info extends AbstractParse {

    private short access_flags;
    private short name_index;
    private short descriptor_index;
    private short attributes_count;
    private Attribute_info attributes[];

    @Override
    public Field_info parse(ClassReader classReader) {
        this.access_flags = classReader.readShort();
        this.name_index = classReader.readShort();
        this.descriptor_index = classReader.readShort();
        this.attributes_count = classReader.readShort();
        this.attributes = Attribute_infoParse.parse(attributes_count, classReader, getClassFile());
        return this;
    }

    /**
     * 获取字段名称
     * @Author alan
     * @Date 2020/12/21 12:21 上午
     * @return
     */
    public String getName() {
        return ((CONSTANT_Utf8_info) getClassFile().getConstant_pool()[name_index]).getValue();
    }

    /**
     * 获取字段描述信息
     * @Author alan
     * @Date 2020/12/21 12:21 上午
     * @return
     */
    public String getDescriptor() {
        return ((CONSTANT_Utf8_info) getClassFile().getConstant_pool()[descriptor_index]).getValue();
    }
}
